﻿using Dal;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    public class UserBll
    {
        public int Add(TUser model)
        {
            using (var db = new BabyfaceEntities())
            {
                var dal = new UserDal(db);
                return dal.Add(model);
            }
        }

        public int Edit(TUser model)
        {
            using (var db = new BabyfaceEntities())
            {
                var dal = new UserDal(db);
                return dal.Edit(model);
            }
        }

        public int Delete(int id)
        {
            using (var db = new BabyfaceEntities())
            {
                var dal = new UserDal(db);
                return dal.Delete(id);
            }
        }

        public TUser GetById(int id)
        {
            using (var db = new BabyfaceEntities())
            {
                var dal = new UserDal(db);
                return dal.GetById(id);
            }
        }

        public IList<TUser> GetAll()
        {
            using (var db = new BabyfaceEntities())
            {
                var dal = new UserDal(db);
                var list = dal.GetAll();
                return list;
            }
        }

        public SysUser Login(TUser model)
        {
            if (model == null)
                return null;
            string LoginName = model.LoginName;
            string LoginPassword = model.LoginPassword;
            if (LoginName == null || LoginName == "" ||
                LoginPassword == null || LoginPassword == "")
                return null;
            DataSet ds;
            using (var db = new BabyfaceEntities())
            {
                var dal = new UserDal(db);
                ds = dal.Login(LoginName, LoginPassword);
            }
            if (ds != null &&
                ds.Tables.Count > 0 &&
                ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToSysUser(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        public SysUser DataRowToSysUser(DataRow row)
        {
            var model = new SysUser();
            if (row != null)
            {
                if (row["TUserId"] != null && row["TUserId"].ToString() != "")
                {
                    model.TUserId = int.Parse(row["TUserId"].ToString());
                }
                if (row["PersonalName"] != null)
                {
                    model.PersonalName = row["PersonalName"].ToString();
                }
                if (row["TRoleId"] != null && row["TRoleId"].ToString() != "")
                {
                    model.TRoleId = int.Parse(row["TRoleId"].ToString());
                }
                if (row["TRoleName"] != null)
                {
                    model.TRoleName = row["TRoleName"].ToString();
                }
                if (row["CompanyId"] != null && row["CompanyId"].ToString() != "")
                {
                    model.CompanyId = int.Parse(row["CompanyId"].ToString());
                }
                if (row["CompanyName"] != null)
                {
                    model.CompanyName = row["CompanyName"].ToString();
                }
            }
            return model;
        }

    }
}
